<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="角色名称" prop="movieName">
        <el-input
          v-model="queryParams.movieName"
          placeholder="请输入影片名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="影片类型" prop="movieType">
        <el-select
          v-model="queryParams.movieType"
          placeholder="影片类型"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in movieTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ciname:movie:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ciname:movie:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ciname:movie:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="movieList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="电影编号" prop="id" width="120"/>
      <el-table-column label="电影名称" prop="movieName" :show-overflow-tooltip="true" width="150"/>
      <el-table-column label="时长(分钟)" prop="duration" :show-overflow-tooltip="true" width="150"/>
      <el-table-column label="类型" prop="movieType" :show-overflow-tooltip="true" width="150">
        <template slot-scope="scope">
          <span>{{ parseType(scope.row.movieType) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="封面" prop="cover" :show-overflow-tooltip="true" width="150">
        <template slot-scope="scope">
          <el-image
            style="width: 50px; height: 100px"
            :src="scope.row.cover"
           :preview-src-list="srcList">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ciname:movie:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ciname:movie:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="电影名称" prop="movieName">
          <el-input v-model="form.movieName" placeholder="请输入电影名称"/>
        </el-form-item>
        <el-form-item label="电影类型" prop="movieType">
          <el-select
            v-model="form.movieType"
            placeholder="影片类型"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option
              v-for="dict in movieTypeOptions"
              :key="dict.value"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="影片时长:">
          <el-input v-model="form.duration" placeholder="请输入影片时长"></el-input>
        </el-form-item>
        <el-form-item label="封面:">
           <ImageUpLoad @input="updateImgUrl" :value="imageUrl" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>


import { listMovie, delMovies, updateMovie, getMovie, addMovie } from '@/api/cinema/movie'
import ImageUpLoad from '@/components/ImageUpload/index'

export default {
  name: 'Moviemng',
  data() {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      disabled: false,
      title: '',
      // 是否显示弹出层
      open: false,
      imageUrl: '',
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        movieName: undefined,
        movieType: undefined
      },
      // 表单校验
      rules: {
        movieName: [
          { required: true, message: '电影名称不能为空', trigger: 'blur' }
        ]
      },
      // 表单参数
      form: {},
      movieTypeOptions: [],
      dateRange: [],
      movieList: [],
      showSearch: true,
      single: true,
      multiple: true,
      total: 0,
      loading: true,
      srcList: []
    }
  },
  components: {
    ImageUpLoad
  },
  created() {
    this.getList()
    this.getDicts('cinema_movie_type').then(response => {
      this.movieTypeOptions = response.data
    })
  },
  methods: {
    updateImgUrl(res){
      this.imageUrl = res.url;
      this.form.cover = res.url;
    },
    /** 查询影片列表 */
    getList() {
      this.loading = true
      listMovie(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.movieList = response.rows
        this.total = response.total
        this.previeListInit(response.rows)
        this.loading = false
      })
    },
    previeListInit(response){
      this.srcList=[]
      for (let i = 0; i < response.length; i++) {
        this.srcList.push(response[i].cover)
      }
      console.log('this.srcList:',this.srcList)
    },
    parseType(value) {
      for (let i = 0; i < this.movieTypeOptions.length; i++) {

        if (value  !== null && this.movieTypeOptions[i].dictValue === value.toString()) {
          return this.movieTypeOptions[i].dictLabel
        }
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加电影'
    },
    // 表单重置
    reset() {
      this.form = {
        movieId: undefined,
        movieName: undefined,
        movieType: undefined,
        cover: ''
      }
      this.imageUrl = ''
      this.resetForm('form')
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const movieId = row.id || this.ids
      getMovie(movieId).then(response => {
        this.form = response.data
        this.imageUrl =this.form.cover
        //为了让编辑的时候 下拉框显示的是字典的value值，查资料发现是类型不匹配的问题，下拉框的value是字符型
        this.form.movieType = response.data.movieType.toString()
        this.open = true
        this.title = '修改电影'
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const movieIds = row.id || this.ids
      this.$confirm('是否确认删除角色编号为"' + movieIds + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delMovies(movieIds)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.roleId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.movieId != undefined) {
            updateMovie(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addMovie(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    handlePreview(file) {
      console.log(file)
    }
  }
}
</script>
